﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using TeachingManageSystem.BLL;
using TeachingManageSystem.Entity;

namespace TeachingManageSystem.Web.Install
{
    public partial class AddBookType : Page
    {
        //首次加载绑定Gridview
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetGridview();
            }
            var n = Request.QueryString["btName"];
            if (n != null)
            {
                IsExsitBookTypeName(n);
            }
        }
        /// <summary>
        /// 添加教材类别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnAddClick(object sender, EventArgs e)
        {
            var bt = new BookType { Name = TbxBookTypeName.Text, Remark = TbxBookTypeRemark.Text };
            BookTypeBll.InsertBookType(bt);
            GetGridview();
            TbxBookTypeName.Text = "";
            TbxBookTypeRemark.Text = "";
        }

        /// <summary>
        /// 删除教材类别
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewBookTypeRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var key = GridViewBookType.DataKeys[e.RowIndex];
            if (key != null)
            {
                var dataKey = key.Value.ToString();
                {
                    var id = Convert.ToInt32(dataKey);
                    BookTypeBll.DeleteBookTypeByID(id);
                    ClientScript.RegisterStartupScript(GetType(), "Message",
                                                       "<SCRIPT LANGUAGE='javascript'>alert('确认删除么？!');</script>");
                }
                GetGridview();
            }
        }

        /// <summary>
        /// GridView分页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void GridViewBookTypePageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridViewBookType.PageIndex = e.NewPageIndex;
            GetGridview();
        }

        protected void GridViewBookTypeRowEditing(object sender, GridViewEditEventArgs e)
        {
            GridViewBookType.EditIndex = e.NewEditIndex;
            GetGridview();
        }

        /// <summary>
        /// 绑定Gridview
        /// </summary>
        public void GetGridview()
        {
            GridViewBookType.DataSource = BookTypeBll.GetBookType();
            GridViewBookType.DataBind();
        }

        //编辑按键下的取消代码。将EditIndex=-1,然后在绑定数据库。
        protected void GridViewBookTypeRowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            GridViewBookType.EditIndex = -1;
            GetGridview();
        }

        protected void GridViewBookTypeRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            var bt = new BookType();
            var dataKey = GridViewBookType.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                bt.ID = (int)dataKey.Value;
            }
            bt.Name = ((TextBox)GridViewBookType.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
            bt.Remark = ((TextBox)GridViewBookType.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
            BookTypeBll.UpdateBookTypeByID(bt);
            GridViewBookType.EditIndex = -1;
            GetGridview();
        }

        public string IsExsitBookTypeName(string btName)
        {
            var bt = new BookType { Name = btName };
            var n = BookTypeBll.CheckBookTypeName(bt);
            Response.Write(n);
            Response.End();
            return n;

        }
    }
}